import { createRouter, createWebHistory } from 'vue-router'
// const routes = 
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/basicMatters',
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/login/login.vue')
    },
    {
      path: '/',
      name: 'index',
      component: () => import('../views/commonLayout/index.vue'),
      children: [
        {
          path: "/basicMatters",
          name: "基本事项",
          component: () => import("../views/BasicMatters/index.vue"),
          meta: { requiresAuth: true }
        },
        {
          path: "/employerInfo",
          name: "员工资料",
          component: () => import("../views/EmployerInfo/index.vue"),
          meta: { requiresAuth: true }
        },
        {
          path: "/rawMaterial",
          name: "原料检测",
          component: () => import("../views/RawMaterial/index.vue"),
          meta: { requiresAuth: true }
        },
        {
          path: "/inboundInspection",
          name: "成品入库检测",
          component: () => import("../views/InboundInspection/index.vue"),
          meta: { requiresAuth: true }
        },
        {
          path: "/itemsToDo",
          name: "待办事项",
          component: () => import("../views/ItemsToDo/index.vue"),
          meta: { requiresAuth: true }
        },
        {
          path: "/outInspection",
          name: "成品出库检测",
          component: () => import("../views/OutInSpection/index.vue"),
          meta: { requiresAuth: true }
        }, {
          path: "/personalInfo",
          name: "个人信息",
          component: () => import("../views/PersonalInfo/index.vue"),
          meta: { requiresAuth: true }
        },
        {
          path: "/summaryMatters",
          name: "事务汇总",
          component: () => import("../views/SummaryMatters/index.vue"),
          meta: { requiresAuth: true }
        },
      ]
    },
  ]
})
// 全局前置守卫  
router.beforeEach((to, from, next) => {
  // 检查目标路由是否需要认证 
  console.log(1111, to.matched);

  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 这里检查用户是否登录，例如：通过 store 或者其他方式
    // 假设有一个全局的 store，并且里面有一个 user 对象  
    const isLoggedIn = localStorage.getItem('loginId') || ''/* 检查用户是否登录的逻辑 */;
    if (!isLoggedIn) {
      next('/login'); //未登录，重定向到登录页面  
    } else {
      next(); //  已登录，继续导航  
    }
  } else {
    next(); //未登录，重定向到登录页面 
  }
})
export default router
